<?php
class Admin_AdminController extends Zend_Controller_Action {
	
     public function init(){
		Zend_Loader::loadClass('AdminModel');
	}
	
    function preDispatch(){
    	/*
    	 * Kiem tra dang nhap
    	 */
		
		$auth = Zend_Auth::getInstance();
		if(strcmp($this->_request->getActionName(),'login')!=0){
			if (!$auth->hasIdentity()) {
				$this->_redirect('/admin/index/login');
			}
		}
		/*
		 * Tra cac thong tin ve moduln cho view
		*/
		$this->view->module = $this->_request->getModuleName();
		$this->view->controller =  $this->_request->getControllerName();
		$this->view->action =  $this->_request->getActionName();
		/*
		 * Load template
		*/
		$layoutPath = APPLICATION_PATH  . '/templates/admin/admin';
		$option = array (	'layout' 		=> 'index',
				'layoutPath' 	=> $layoutPath );
		Zend_Layout::startMvc ( $option );
	}
	
	public function accountAction(){
	//	echo 12;die;
		$dt = new AdminModel();
		$requestnew = $this->_request;
		$request = $this->getRequest();
		if ($this->_request->isPost()){
			$id = $request->getParam('id');
			$name = $request->getParam('name');
			$email = $request->getParam('email');
			$username = $request->getParam('username');
			$password = $request->getParam('password');
		//	echo $username.$password;die;
			if($name != "" && $email != "" && $username && $password){
				$password = md5($password);
				if((trim($name) != "") && (trim($email) != "")){
				   $email = trim($email);
				   $data = array("name" => $name,
				                 "email" => $email,
				   				 "username" => $username,
				   				 "password" => $password);
				   if($requestnew->getParam('submit') == 'Thêm tài khoản'){
				   		$dt->insert($data);
			       }
			       else if($requestnew->getParam('submit') == 'Chỉnh sửa'){
				   		 $dt->update($data,"id=".$id);
			       }
			       $this->view->success = 1;
				}
			}
			else{
				$this->view->warning = 1;
			}
		}
		$this->view->datas = $dt->admin();
		
	}
	
	public function deleteacountAction(){
		$dt = new AdminModel();
		$request = $this->getRequest();
		$id = $request->getParam('id');
		if($id){
			$dt->delete("id=".$id);
		}
		exit();
	}
	
    public function editacountAction(){
    	$layout = $this->_helper->layout();
		$layout->disableLayout();
		$request = $this->getRequest();
		$id = $request->getParam('id');
		$dt = new AdminModel();
		$this->view->data = $dt->id_admin($id);
	}
	 
	public function editstatusAction(){
		$layout = $this->_helper->layout();
		$layout->disableLayout();
		$dt = new AdminModel();
		$request = $this->getRequest();
		$id = $request->getParam('id');
		$status = $request->getParam('status');
		if($id){
			if($status == 1) $status=0;
			else if($status ==0) $status=1;
			$data = array("status" => $status);
			$dt->update($data,"id=".$id);
			$this->view->data = $dt->id_admin($id);
		}
		
	}
}






